﻿Imports doan_1065019_1065001.BaoCaoHangThangDAO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Public Class frmXuatBaoCao


    Dim t As Integer = 0
    Dim n As Integer = 0
    Private Sub frmXuatBaoCao_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        n = Date.Now.Year()
        t = Integer.Parse(AppSettingsWriter.GetValue("ThangLapBaoCao"))

        If (t <> 0) Then
            If (Date.Now.Month() = 1) Then
                n = n - 1
                t = Date.Now.Month()
            Else
                t = Date.Now.Month() - 1
            End If

        Else
            t = Date.Now.Month()
        End If


        cbNam.Text = n
        cbThang.Text = t
    End Sub

    Private Sub btnXuat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXuat.Click

        Dim thang As Integer = Integer.Parse(cbThang.Text)
        Dim nam As Integer = Integer.Parse(cbNam.Text)
        Dim sql As String = String.Format("exec Update_ThangNam {0},{1},{2}", 1, thang, nam)
        SqlDataProvider.NonQuery(sql)
        Dim dt As DataTable = BaoCaoHangThangBUS.GetMaBaoCaoHangThangBUS(thang, nam)
        If (dt.Rows.Count > 0) Then
            Dim rpt As CrystalReport1 = New CrystalReport1()
            CrystalReportViewer1.ReportSource = rpt
        Else
            If (thang = t And nam = n) Then
                Dim sql1 As String = String.Format("exec Create_BaoCaoHangThang {0},{1}", thang, nam)
                SqlDataProvider.NonQuery(sql1)
                Dim rpt As CrystalReport1 = New CrystalReport1()
                CrystalReportViewer1.ReportSource = rpt
            Else
                MessageBox.Show("Không tồn tại dữ liệu")
            End If

        End If
    End Sub

    Private Sub btnHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Try


            Dim thang As Integer = Integer.Parse(cbThang.Text)
            Dim nam As Integer = Integer.Parse(cbNam.Text)
            Dim sql As String = String.Format("exec Update_ThangNam {0},{1},{2}", 1, thang, nam)
            SqlDataProvider.NonQuery(sql)
            Dim dt As DataTable = BaoCaoHangThangBUS.GetMaBaoCaoHangThangBUS(thang, nam)
            If (dt.Rows.Count > 0) Then
                BaoCaoHangThangBUS.DeleteBaoCaoHangThangBUS(thang, nam)
                MessageBox.Show("Hủy thành công")
            Else

                MessageBox.Show("Không tồn tại dữ liệu")
            End If
        Catch ex As Exception
            MessageBox.Show("Đã xảy ra lỗi, bạn vui lòng coi lại kết nối")
        End Try
    End Sub

    Private Sub TrangChủToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrangChủToolStripMenuItem.Click
        Me.Close()
    End Sub


End Class